﻿using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Context
{
    public class EducationContext : DbContext
    {

        public EducationContext(DbContextOptions<EducationContext> options) : base(options) { }

        public DbSet<Admin> admin { get; set; }
        public DbSet<Classes> classes { get; set; }
        public DbSet<Classroom> classroom { get; set; }
        public DbSet<Course> courses { get; set; }
        public DbSet<CourseScheduling> courseSchedulings { get; set; }
        public DbSet<CourseSelect> courseSelects { get; set; }
        public DbSet<Group> groups { get; set; }
        public DbSet<LearningMaterial> learningMaterials { get; set; }
        public DbSet<Teacher> teachers { get; set; }
        public DbSet<User> users { get; set; }
        public DbSet<UserClass> userClasses { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            //learningMaterials与teacher的一对一外键
            builder.Entity<LearningMaterial>()
                .HasOne(learningMaterials => learningMaterials.Teacher)
                .WithOne(teachers => teachers.LearningMaterial)
                .HasForeignKey<LearningMaterial>(learningMaterials => learningMaterials.Grade);

            //learningMaterials与teacher的一对一外键
            builder.Entity<Teacher>()
                .HasOne(teachers => teachers.LearningMaterial)
                .WithOne(learningMaterials => learningMaterials.Teacher)
                .HasForeignKey<Teacher>(teachers => teachers.Grade);

            //软删除过滤
            builder.Entity<Classes>(option => option.HasQueryFilter(e => !e.IsDelete));
            builder.Entity<Classroom>(option => option.HasQueryFilter(e => !e.IsDelete));
            builder.Entity<Course>(option => option.HasQueryFilter(e => !e.IsDelete));
            builder.Entity<CourseScheduling>(option => option.HasQueryFilter(e => !e.IsDelete));
            builder.Entity<CourseSelect>(option => option.HasQueryFilter(e => !e.IsDelete));
            builder.Entity<Group>(option => option.HasQueryFilter(e => !e.IsDelete));
            builder.Entity<LearningMaterial>(option => option.HasQueryFilter(e => !e.IsDelete));
            builder.Entity<Teacher>(option => option.HasQueryFilter(e => !e.IsDelete));
            builder.Entity<User>(option => option.HasQueryFilter(e => !e.IsDelete));
            builder.Entity<UserClass>(option => option.HasQueryFilter(e => !e.IsDelete));


            //管理员表数据种子
            builder.Entity<Admin>().HasData(
                new Admin { AdminId = 1, AdminName = "Admin A", AdminHead = "jpg.png", AdminPhone = "114514999", AdminPwd = "root" }
                );

            //班级表数据种子
            builder.Entity<Classes>().HasData(
                new Classes { ClassId = 1, ClassName = "Class A" },
                new Classes { ClassId = 2, ClassName = "Class B" },
                new Classes { ClassId = 3, ClassName = "Class C" },
                new Classes { ClassId = 4, ClassName = "Class D" }
            );

            //分组表数据种子
            builder.Entity<Group>().HasData(
                new Group { GroupId = 1, GroupName = "Group A" },
                new Group { GroupId = 2, GroupName = "Group B" },
                new Group { GroupId = 3, GroupName = "Group C" },
                new Group { GroupId = 4, GroupName = "Group D" }
            );

            //用户表数据种子
            builder.Entity<User>().HasData(
                new User { UserId = 1, GroupId = 1, Gender = true, UserName = "User A", UserPhone = "1234567890", Pwd = "password1", UserHead = "avatar1.jpg" },
                new User { UserId = 2, GroupId = 3, Gender = false, UserName = "User B", UserPhone = "9876543210", Pwd = "password2", UserHead = "avatar2.jpg" },
                new User { UserId = 3, GroupId = 2, Gender = true, UserName = "User C", UserPhone = "0123456789", Pwd = "password3", UserHead = "avatar3.jpg" },
                new User { UserId = 4, GroupId = 4, Gender = false, UserName = "User D", UserPhone = "9876540123", Pwd = "password4", UserHead = "avatar4.jpg" }
            );

            //教室表数据种子
            builder.Entity<Classroom>().HasData(
                new Classroom { ClassroomId = 1, ClassroomName = "Classroom A", ClassroomPath = "Building A, Room 101" },
                new Classroom { ClassroomId = 2, ClassroomName = "Classroom B", ClassroomPath = "Building B, Room 201" },
                new Classroom { ClassroomId = 3, ClassroomName = "Classroom C", ClassroomPath = "Building C, Room 301" },
                new Classroom { ClassroomId = 4, ClassroomName = "Classroom D", ClassroomPath = "Building D, Room 401" }
            );

            //课程表数据种子
            builder.Entity<Course>().HasData(
                new Course { CourseId = 1, CourseName = "Course A" },
                new Course { CourseId = 2, CourseName = "Course B" },
                new Course { CourseId = 3, CourseName = "Course C" },
                new Course { CourseId = 4, CourseName = "Course D" }
            );

            //排课表数据种子
            builder.Entity<CourseScheduling>().HasData(
                new CourseScheduling
                {
                    CourseSchedulingId = 1,
                    CourseId = 4,
                    ClassId = 1,
                    TeacherId = 3,
                    ClassroomId = 1,
                    LearningMaterialId = 2,
                    ClassBegin = DateTime.Now,
                    ClassEnd = DateTime.Now.AddHours(1)
                },
                new CourseScheduling
                {
                    CourseSchedulingId = 2,
                    CourseId = 3,
                    ClassId = 4,
                    TeacherId = 2,
                    ClassroomId = 1,
                    LearningMaterialId = 2,
                    ClassBegin = DateTime.Now.AddDays(1),
                    ClassEnd = DateTime.Now.AddDays(1).AddHours(1)
                },
                new CourseScheduling
                {
                    CourseSchedulingId = 3,
                    CourseId = 4,
                    ClassId = 3,
                    TeacherId = 2,
                    ClassroomId = 1,
                    LearningMaterialId = 3,
                    ClassBegin = DateTime.Now.AddDays(2),
                    ClassEnd = DateTime.Now.AddDays(2).AddHours(1)
                },
                new CourseScheduling
                {
                    CourseSchedulingId = 4,
                    CourseId = 4,
                    ClassId = 3,
                    TeacherId = 2,
                    ClassroomId = 4,
                    LearningMaterialId = 1,
                    ClassBegin = DateTime.Now.AddDays(3),
                    ClassEnd = DateTime.Now.AddDays(3).AddHours(1)
                }
            );

            //选课表数据种子
            builder.Entity<CourseSelect>().HasData(
                new CourseSelect
                {
                    CourseSelectId = 1,
                    TeacherId = 3,
                    ClassesId = 1,
                    CourseId = 4
                },
                new CourseSelect
                {
                    CourseSelectId = 2,
                    TeacherId = 3,
                    ClassesId = 1,
                    CourseId = 3
                },
                new CourseSelect
                {
                    CourseSelectId = 3,
                    TeacherId = 4,
                    ClassesId = 2,
                    CourseId = 3
                },
                new CourseSelect
                {
                    CourseSelectId = 4,
                    TeacherId = 3,
                    ClassesId = 1,
                    CourseId = 4
                }
            );

            //学习资料表数据种子
            builder.Entity<LearningMaterial>().HasData(
                new LearningMaterial
                {
                    LearningMaterialId = 1,
                    Grade = 3,
                    MaterialName = "资料1",
                    MaterialPath = "路径1"
                },
                new LearningMaterial
                {
                    LearningMaterialId = 2,
                    Grade = 1,
                    MaterialName = "资料2",
                    MaterialPath = "路径2"
                },
                new LearningMaterial
                {
                    LearningMaterialId = 3,
                    Grade = 2,
                    MaterialName = "资料3",
                    MaterialPath = "路径3"
                },
                new LearningMaterial
                {
                    LearningMaterialId = 4,
                    Grade = 4,
                    MaterialName = "资料4",
                    MaterialPath = "路径4"
                }
            );

            //教师表数据种子
            builder.Entity<Teacher>().HasData(
                new Teacher
                {
                    TeacherId = 1,
                    TeacherName = "教师1",
                    TeacherPhone = "手机号码1",
                    TeacherHead = "头像1",
                    TeacherPwd = "密码1",
                    Grade = 1
                },
                new Teacher
                {
                    TeacherId = 2,
                    TeacherName = "教师2",
                    TeacherPhone = "手机号码2",
                    TeacherHead = "头像2",
                    TeacherPwd = "密码2",
                    Grade = 2
                },
                new Teacher
                {
                    TeacherId = 3,
                    TeacherName = "教师3",
                    TeacherPhone = "手机号码3",
                    TeacherHead = "头像3",
                    TeacherPwd = "密码3",
                    Grade = 3
                },
                new Teacher
                {
                    TeacherId = 4,
                    TeacherName = "教师4",
                    TeacherPhone = "手机号码4",
                    TeacherHead = "头像4",
                    TeacherPwd = "密码4",
                    Grade = 4
                }
            );

            //用户班级表数据种子
            builder.Entity<UserClass>().HasData(
                new UserClass
                {
                    UserClassId = 1,
                    UserId = 3,
                    ClassesId = 2
                },
                new UserClass
                {
                    UserClassId = 2,
                    UserId = 4,
                    ClassesId = 1
                },
                new UserClass
                {
                    UserClassId = 3,
                    UserId = 2,
                    ClassesId = 2
                },
                new UserClass
                {
                    UserClassId = 4,
                    UserId = 4,
                    ClassesId = 2
                }
            );
        }

    }
}
